package com.jinmdz.fmis.dao.admin.dao;

import com.jinmdz.fmis.dao.admin.dao.provider.GoodsServiceProvider;
import com.jinmdz.fmis.dao.admin.model.goodsservice.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.ArrayList;

/**
 * 服务物品Dao
 *
 * @author GuanKui
 * @date 2020/3/9 15:13
 */
public interface GoodsServiceDao {

    /**
     * 获取物品、服务类型基本信息
     *
     * @param
     * @return
     * @author GuanKui
     * @date 2020/3/9  15:35
     */
    @Select(" select id,parent_id,type_code,type_name,type_describe,sort,remark,version " +
            " from service_type where deleted = 0 order by sort  ")
    ArrayList<GoodsServiceTypeListItem> listGoodsServiceTypeList();

    /**
     * 根据id获取物品、服务类型基本信息
     *
     * @param id 表id
     * @return
     * @author GuanKui
     * @date 2020/3/9  17:06
     */
    @Select(" select id,parent_id,type_code,type_name,type_describe,sort,remark,version " +
            " from service_type where id = #{id} and deleted = 0 limit 1  ")
    GoodsServiceTypeEditItem getGoodsServiceTypeEditById(@Param("id") Integer id);

    /**
     * 根据parent_id获取物品、服务类型基本信息
     *
     * @param parentId 父记录ID
     * @return
     * @author GuanKui
     * @date 2020/3/9  15:35
     */
    @Select(" select id,parent_id,type_code,type_name,type_describe,sort,remark,version " +
            "from service_type where parent_id=#{parentId} and deleted = 0  order by sort  desc limit 1  ")
    GoodsServiceTypeEditItem getGoodsServiceTypeByParentId(@Param("parentId") Integer parentId);

    /**
     * 按照类型编码获取非当前物品服务类型主键
     *
     * @param typeId   当前类型主键
     * @param typeCode 类型编码
     * @return
     * @author LiCongLu
     * @date 2020-03-26 10:14
     */
    @Select(" select id from service_type where id != IFNULL(#{typeId},0) and type_code = #{typeCode} and deleted = 0 limit 1 ")
    @ResultType(Integer.class)
    Integer getGoodsServiceTypeIdByTypeCode(@Param("typeId") Integer typeId, @Param("typeCode") String typeCode);

    /**
     * 按照类型名称获取非当前物品服务类型主键
     *
     * @param typeId   当前类型主键
     * @param typeName 类型名称
     * @return
     * @author LiCongLu
     * @date 2020-03-26 10:14
     */
    @Select(" select id from service_type where id != IFNULL(#{typeId},0) and type_name = #{typeName} and deleted = 0 limit 1 ")
    @ResultType(Integer.class)
    Integer getGoodsServiceTypeIdByTypeName(@Param("typeId") Integer typeId, @Param("typeName") String typeName);

    /**
     * 按照类型查询物品服务个数
     *
     * @param typeId 类型主键
     * @return
     * @author LiCongLu
     * @date 2020-03-26 13:06
     */
    @Select(" select count(*) from service_item where service_type_id = #{typeId} and deleted = 0 ")
    @ResultType(Integer.class)
    Integer getServiceCountByTypeId(@Param("typeId") Integer typeId);

    /**
     * 根据service_type_id获取服务项目信息
     *
     * @param typeId  服务分类外键
     * @param keyword 关键字
     * @return
     * @author GuanKui
     * @date 2020/3/9  15:35
     */
    @SelectProvider(type = GoodsServiceProvider.class, method = "listGoodsServiceListByTypeId")
    ArrayList<GoodsServiceListItem> listGoodsServiceListByTypeId(@Param("typeId") Integer typeId, @Param("keyword") String keyword);

    /**
     * 查询不是任务包的物品、服务信息
     *
     * @param
     * @return
     * @author GuanKui
     * @date 2020/3/9  15:35
     */
    @SelectProvider(type = GoodsServiceProvider.class, method = "listGoodsServiceListNoPackage")
    ArrayList<GoodsServiceListItem> listGoodsServiceListNoPackage();

    /**
     * 按照项目编码获取非当前物品服务主键
     *
     * @param itemId        当前物品服务主键
     * @param serviceTypeId 物品分类主键
     * @param itemCode      项目编码
     * @return
     * @author LiCongLu
     * @date 2020-03-26 15:03
     */
    @Select(" select id from service_item where id != IFNULL(#{itemId},0) and service_type_id = #{serviceTypeId} and item_code = #{itemCode} and deleted = 0 limit 1 ")
    @ResultType(Integer.class)
    Integer getGoodsServiceIdByItemCode(@Param("itemId") Integer itemId, @Param("serviceTypeId") Integer serviceTypeId, @Param("itemCode") String itemCode);

    /**
     * 按照项目名称获取非当前物品服务主键
     *
     * @param itemId        当前物品服务主键
     * @param serviceTypeId 物品分类主键
     * @param itemName      项目名称
     * @return
     * @author LiCongLu
     * @date 2020-03-26 15:03
     */
    @Select(" select id from service_item where id != IFNULL(#{itemId},0) and service_type_id = #{serviceTypeId} and item_name = #{itemName} and deleted = 0 limit 1 ")
    @ResultType(Integer.class)
    Integer getGoodsServiceIdByItemName(@Param("itemId") Integer itemId, @Param("serviceTypeId") Integer serviceTypeId, @Param("itemName") String itemName);

    /**
     * 按照主键获取物品服务信息
     *
     * @param id 主键
     * @return
     * @author LiCongLu
     * @date 2020-03-26 15:24
     */
    @SelectProvider(type = GoodsServiceProvider.class, method = "getGoodsServiceEditById")
    GoodsServiceEditItem getGoodsServiceEditById(@Param("id") Integer id);

    /**
     * 根据serviceItem表Id删除服务包关联信息
     *
     * @param packageId      物品包主键
     * @param modifiedUserId 修改主键
     * @param
     * @return
     * @author GuanKui
     * @date 2020/3/13  11:22
     */
    @Select(" update service_package_item set deleted = 1,modified_user_id = #{modifiedUserId},version = unix_timestamp(now()) " +
            " where service_package_id = #{packageId}  and deleted = 0 ")
    void deleteServicePackageForPackageId(@Param("packageId") Integer packageId, @Param("modifiedUserId") Integer modifiedUserId);

    /**
     * 根据service_package_id获取服务包信息
     *
     * @param packageId 主键
     * @return
     * @author GuanKui
     * @date 2020/3/9  15:35
     */
    @Select(" select service_item_id,service_item.item_name,service_package_item.item_number " +
            " from service_package_item join service_item on service_item.id = service_item_id " +
            " where service_package_id = #{packageId} and service_package_item.deleted = 0 and service_item.deleted = 0 ")
    ArrayList<GoodsServicePackageListItem> listGGoodsServicePackageListByPackageId(@Param("packageId") Integer packageId);

    /**
     * 根据serviceItemId获取服务包信息
     *
     * @param serviceItemId serviceItem表ID
     * @return
     * @author GuanKui
     * @date 2020/3/10 14:26
     */
    @Select(" select service_item_id from service_package_item where service_item_id = #{serviceItemId} and deleted = 0 ")
    @ResultType(Integer.class)
    Integer getServicePackageIdByServiceItemId(@Param("serviceItemId") Integer serviceItemId);

    /**
     * 查询所有有效物品、服务信息
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-03-27 09:51
     */
    @SelectProvider(type = GoodsServiceProvider.class, method = "listGoodsServiceListAsValid")
    ArrayList<GoodsServiceListItem> listGoodsServiceListAsValid();
}
